package com.minotauro.bean.property;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.AbstractProcessor;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import org.apache.commons.io.FilenameUtils;

import com.minotauro.i18n.scanner.IsProcessable;

public class BeanIsProcessable implements IsProcessable {

  // --------------------------------------------------------------------------------

  @Override
  public Boolean isProcessable(File file, List<String> path) {
    if (!FilenameUtils.getExtension(file.getName()).equals("java")) {
      return false;
    }

    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();

    StandardJavaFileManager standardJavaFileManager = //
        javaCompiler.getStandardFileManager(null, null, null);

    List<File> fileList = new ArrayList<File>();
    fileList.add(file);

    Iterable<? extends JavaFileObject> compilationUnits = //
        standardJavaFileManager.getJavaFileObjectsFromFiles(fileList);

    CompilationTask task = javaCompiler.getTask( //
        null, standardJavaFileManager, null, null, null, compilationUnits);

    AnnotationProcessor annotationProcessor = new AnnotationProcessor();

    List<AbstractProcessor> processorList = new ArrayList<AbstractProcessor>();
    processorList.add(annotationProcessor);

    task.setProcessors(processorList);

    task.call();

    return annotationProcessor.getIsCledaBean();
  }
}
